﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Library.Common;
using Library.Bll;
namespace HanhuaMicroCredit.BLL
{
    /*
    public class treeBll
    {
        public object destType { get; set; }
        public object busiType { get; set; }
        private const string _tree = "Base_ProTree";
        public treeBll()
        {
        }
        public static treeBll Instance
        {
            get
            {
                return new treeBll();
            }
        }
        /// <summary>
        /// 获取子节点菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private List<Dictionary<string, object>> getTreeListById(string id)
        {
            string selectFields = "id,title";
            StringBuilder conditions = new StringBuilder();
            conditions.Append(" isValid = 1");
            conditions.AppendFormat(" and destType={0} ", Utils.StrToInt(destType, 0).ToString());
            conditions.AppendFormat(" and busiType={0} ", Utils.StrToInt(busiType, 0).ToString());
            conditions.AppendFormat(" and parentId={0} ", Utils.StrToInt(id, 0).ToString());
            string orderFields = "orderSeq";
            return CommonBLL.GetList(_tree, selectFields, orderFields, conditions.ToString());
        }

        /// <summary>
        /// 获取目的地树
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<Dictionary<string, object>> getDestTreeListById(string id)
        {
            string selectFields = "id,title";
            StringBuilder conditions = new StringBuilder();
            conditions.Append(" isValid = 1");
            conditions.AppendFormat(" and parId={0} ", Utils.StrToInt(id, 0).ToString());
            string orderFields = "[order]";
            return CommonBLL.GetList("dest_tree", selectFields, orderFields, conditions.ToString());
        }
        /// <summary>
        /// 返回所有后台目录树
        /// </summary>
        /// <returns></returns>
        public string getFirstTree() {
            return getChildTreeById("0", 0);
        }
        /// <summary>
        /// 遍历所有子节点菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="i"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public string getChildTreeById(string id,int i)
        {
            StringBuilder sbHtml = new StringBuilder();
            List<Dictionary<string, object>> list = getTreeListById(id);
            foreach (Dictionary<string, object> d in list)
            {
                string ids = d["id"].ToString();
                string name = "├" + Utils.GetSpacesString(i, "──") + d["title"].ToString();
                sbHtml.AppendFormat("<option value=\"{0}\">{1}</option>", ids, name);
                sbHtml.Append(getChildTreeById(ids, i + 1));
            }
            return sbHtml.ToString();
        }

        

        public string getDestChildTreeById(string id, int i, string selectTreeId)
        {
            StringBuilder sbHtml = new StringBuilder();
            List<Dictionary<string, object>> list = getDestTreeListById(id);
            foreach (Dictionary<string, object> d in list)
            {
                string ids = d["id"].ToString();
                string name = "├" + Utils.GetSpacesString(i, "──") + d["title"].ToString();
                if (selectTreeId == ids)
                {
                    sbHtml.AppendFormat("<option value=\"{0}\" selected>{1}</option>", ids, name); 
                }
                else
                {
                    sbHtml.AppendFormat("<option value=\"{0}\">{1}</option>", ids, name);
                }
                sbHtml.Append(getDestChildTreeById(ids, i + 1, selectTreeId));
            }
            return sbHtml.ToString();
        }


        /// <summary>
        /// 单表遍历所有子节点生成树
        /// </summary>
        public string getChildTreeByPid(string tableName,string pid,string pidval, string selectColumns, string orderName,string orderby,string onclick,string name)
        {
            string conditions=string.Format(" {0}={1} AND IsValid=1 ",pid,pidval);
            string sortColumns = orderName + " " + orderby;
            StringBuilder sbHtml = new StringBuilder();
            List<Dictionary<string, object>> list = CommonBLL.GetList(tableName, selectColumns, sortColumns, conditions);
            if (list != null && list.Count > 0)
            {
                foreach (Dictionary<string, object> d in list)
                {
                    string ids = d["id"].ToString();
                    string parentid = d[pid].ToString(); 
                    string child= getChildTreeByPid(tableName, pid, ids, selectColumns, orderName, orderby,onclick,name);
                    if ("NOT" != child)
                    {
                        sbHtml.Append(string.Format(@"<li><a onclick={0}({1},1)>{2}</a>", onclick, ids, d[name].ToString()));
                        sbHtml.Append(@"<ul>");
                        sbHtml.Append(child);
                        sbHtml.Append(@"</ul>");
                    }
                    else
                    {
                        sbHtml.Append(string.Format(@"<li><a onclick={0}({1},0)>{2}</a>", onclick, ids, d[name].ToString()));
                    }
                    sbHtml.Append(@"</li>");
                }
            }
            else 
            {
                sbHtml.Append("NOT");
            }
            return sbHtml.ToString();
        }
        
        /// <summary>
        /// 单表遍历所有子节点生成下拉列表
        /// </summary>
        public string getSelectTree(string tableName, string tid, string tidval, string selectColumns, string orderName, string orderby,string line,int linenum,string click,string name)
        {
            string conditions = string.Format(" {0}={1} AND IsValid=1 ", tid, tidval);
            string sortColumns = orderName + " " + orderby;
            if ("0" != tidval) { linenum++; }
            StringBuilder sbHtml = new StringBuilder();
            List<Dictionary<string, object>> list = CommonBLL.GetList(tableName, selectColumns, sortColumns, conditions);
            if (list != null && list.Count > 0)
            {
                foreach (Dictionary<string, object> d in list)
                {
                    string ids = d["id"].ToString();
                    string parentid = d[tid].ToString();
                    
                    StringBuilder opname = new StringBuilder();
                    opname.Append("├");
                    for (int i = 0; i < linenum; i++)
                    {
                        opname.Append(line);
                    }
                    opname.Append(d[name].ToString());
                    if (ids == click)
                    {
                        sbHtml.Append(string.Format(@"<option value='{0}' selected='selected'>{1}</option>", ids, opname.ToString()));
                    }
                    else
                    {
                        sbHtml.Append(string.Format(@"<option value='{0}'>{1}</option>", ids, opname.ToString()));
                    }
                    string child = getSelectTree(tableName, tid, ids, selectColumns, orderName, orderby, line,linenum,click,name);
                    if ("NOT" != child)
                    {
                        sbHtml.Append(child);
                    }
                }
            }
            else
            {
                sbHtml.Append("NOT");
            }
            return sbHtml.ToString();
        }

        /// <summary>
        /// 单表生成下拉列表
        /// </summary>
        public string getListByPid(string tableName, string selectColumns, string orderName, string orderby,string name,string select)
        {
            string conditions = " IsValid=1 ";
            string sortColumns = orderName + " " + orderby;
            StringBuilder sbHtml = new StringBuilder();
            List<Dictionary<string, object>> list = CommonBLL.GetList(tableName, selectColumns, sortColumns, conditions);
            if (list != null && list.Count > 0)
            {
                foreach (Dictionary<string, object> d in list)
                {
                    string ids = d["id"].ToString();
                    if (ids == select)
                    {
                        sbHtml.Append(string.Format(@"<option value='{0}' selected='selected'>{1}</option>", ids, d[name].ToString()));
                    }
                    else
                    {
                        sbHtml.Append(string.Format(@"<option value='{0}'>{1}</option>", ids, d[name].ToString()));
                    }
                }
            }
            else
            {
                sbHtml.Append("<li>抱歉，未获得任何数据</li>");
            }
            return sbHtml.ToString();
        }
        /// <summary>
        /// 单表生成下拉列表
        /// </summary>
        public string getListByPid(string tableName, string selectColumns, string orderName, string orderby,string select)
        {
            string conditions = " IsValid=1 ";
            string sortColumns = orderName + " " + orderby;
            StringBuilder sbHtml = new StringBuilder();
            List<Dictionary<string, object>> list = CommonBLL.GetList(tableName, selectColumns, sortColumns, conditions);
            if (list != null && list.Count > 0)
            {
                foreach (Dictionary<string, object> d in list)
                {
                    string ids = d["id"].ToString();
            string destinationName = selectControlBLL.getNameById("id", d["VisaCountry"].ToString(), "Dest_Destination", "destinationName");
            string visatype = selectControlBLL.getNameById("id", d["visatype"].ToString(), "Sys_IBDD", "name");
            string visaarea = selectControlBLL.getNameById("id", d["visaarea"].ToString(), "Sys_IBDD", "name");
            string name = destinationName + "_" + visatype + "_" + visaarea;

            if (ids == select)
            {
                sbHtml.Append(string.Format(@"<option value='{0}' selected='selected'>{1}</option>", ids, name));
            }
            else
            {
                sbHtml.Append(string.Format(@"<option value='{0}'>{1}</option>", ids, name));
            }

                }
            }
            else
            {
                sbHtml.Append("<li>抱歉，未获得任何数据</li>");
            }
            return sbHtml.ToString();
        }
        
    }*/
}
